package com.example.w_library.log.priter.view;

import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.example.w_library.utils.DisplayUtil;

/**
 * 日志可视化显示关闭辅助类
 */
public class WViewPrinterProvider {

    private FrameLayout rootView;

    private View floatingView;

    private boolean isOpen ;

    private FrameLayout logView;

    private RecyclerView recyclerView;

    public WViewPrinterProvider(FrameLayout rootView,RecyclerView recyclerView){
        this.rootView = rootView;
        this.recyclerView = recyclerView;
    }

    private static final String TAG_FLOATING_VIEW = "TAG_FLOATING_VIEW";
    private static final String TAG_LOG_VIEW = "TAG_LOG_VIEW";

    /**
     * 显示 FloatingView
     */
    public void showFloatingView(){
        if (rootView.findViewWithTag(TAG_FLOATING_VIEW) != null){
            return;
        }

        FrameLayout.LayoutParams layoutParams =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        layoutParams.gravity = Gravity.BOTTOM | Gravity.END;

        View floatingView = getFloatingView();

        floatingView.setTag(TAG_FLOATING_VIEW);
        floatingView.setBackgroundColor(Color.RED);
        floatingView.setAlpha(0.8f);
        layoutParams.bottomMargin = DisplayUtil.dp2Px(rootView.getContext(),100);
        rootView.addView(floatingView,layoutParams);
    }

    /**
     * 隐藏 FloatingView
     */
    public void closedFloatingView(){
        rootView.removeView(getFloatingView());
    }


    /**
     * 获取 FloatingView
     * @return
     */
    private View getFloatingView() {

        if (floatingView != null){
            return floatingView;
        }

        TextView textView = new TextView(recyclerView.getContext());
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (!isOpen){
                    showLogView();
                }
            }
        });

        textView.setText("显示 Log");

        return floatingView = textView;
    }

    /**
     * 显示 logView
     */
    private void showLogView(){

        if (rootView.findViewWithTag(TAG_LOG_VIEW) != null){
            return;
        }

        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DisplayUtil.dp2Px(recyclerView.getContext(),260));

        params.gravity = Gravity.BOTTOM;

        View logView = getLogView();

        logView.setTag(TAG_LOG_VIEW);

        rootView.addView(logView,params);

        isOpen = true;
    }

    /**
     * 实例化 logView
     * @return
     */
    private View getLogView() {
        if (logView != null){
            return logView;
        }
        FrameLayout view = new FrameLayout(rootView.getContext());

        view.setBackgroundColor(Color.BLACK);
        view.addView(recyclerView);

        FrameLayout.LayoutParams closeParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        closeParams.gravity = Gravity.END;
        TextView closeView = new TextView(rootView.getContext());
        closeView.setLayoutParams(closeParams);
        closeView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeLogView();
            }
        });
        closeView.setText("关闭 Log");

        view.addView(closeView);

        return logView = view;
    }

    /**
     * 关闭 LogView
     */
    public void closeLogView(){

        isOpen = false;
        rootView.removeView(getLogView());
    }


}
